package com.alinesno.cloud.initializr.starter.gateway.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alinesno.cloud.base.boot.entity.ManagerCodeEntity;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.AjaxResult;
import com.alinesno.cloud.initializr.starter.entity.BuildApplicationTemplatesEntity;
import com.alinesno.cloud.initializr.starter.entity.ProjectInfoEntity;
import com.alinesno.cloud.initializr.starter.gateway.aop.DataFilter;
import com.alinesno.cloud.initializr.starter.service.IBuildApplicationTemplatesService;
import com.alinesno.cloud.initializr.starter.service.IBuildProjectInfoService;
import com.alinesno.cloud.rest.gateway.adapter.page.TableDataInfo;
import com.alinesno.cloud.rest.gateway.adapter.plugins.TranslateCode;
import com.alinesno.cloud.rest.gateway.adapter.rest.BaseRestAdapter;
import com.alinesno.cloud.rest.gateway.consumer.AuthorityPlatformClient;

/**
 * 【请填写功能名称】Controller
 *
 * @author alinesno
 * @date 2021-06-05
 */
@RestController
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("com/alinesno/cloud/business/buildApplication")
public class BuildApplicationRest extends BaseRestAdapter<ProjectInfoEntity, IBuildProjectInfoService> {

    //日志记录
    private static final Logger log = LoggerFactory.getLogger(BuildApplicationRest.class);

    @Autowired
    private IBuildProjectInfoService buildProjectInfoService;

    @Autowired
    private IBuildApplicationTemplatesService templatesService;

    @Autowired
    private AuthorityPlatformClient managerCodeService;

    @TranslateCode
    @ResponseBody
    @DataFilter
    @PostMapping("/datatables")
    public TableDataInfo datatables(HttpServletRequest request, Model model, DatatablesPageBean page) {
        log.debug("page = {}", ToStringBuilder.reflectionToString(page));
        return this.toPage(model, this.getFeign(), page);
    }

    /**
     * 初始化应用
     *
     * @return
     */
    @GetMapping("/initComponent")
    public AjaxResult initComponent() {

        // 获取业务属性
        List<ManagerCodeEntity> attrList = managerCodeService.codeListByType("initializr.admin.project.type");

        // 获取应用模板
        List<BuildApplicationTemplatesEntity> templateList = templatesService.list();

        AjaxResult result = AjaxResult.success();
        result.put("attrList", attrList);
        result.put("templateList", templateList);

        return result;
    }

    @Override
    public IBuildProjectInfoService getFeign() {
        return this.buildProjectInfoService;
    }

}
